/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbchess.util;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;

/**
 *
 * @author Tyler
 */
public class ChessTimer extends javax.swing.JLabel implements Runnable {

        private long startSeconds;
        private long hours = 0;
        private long minutes = 0;
        private long seconds = 0;
        private long realseconds = 0;
        private Thread timer;
        private boolean pause = true;

        public ChessTimer(long startMinutes) {

            startSeconds = startMinutes*60;
            realseconds = startSeconds;

            setFont(new java.awt.Font("SERIF", java.awt.Font.BOLD, 20 ));
            setPreferredSize(new Dimension(getWidth() + 2, getHeight() + 17));
            setVerticalAlignment(CENTER);
            setHorizontalAlignment(CENTER);
            setText(formatTime(realseconds));
            setText(formatTime(startSeconds));

            timer = new Thread(this);
            timer.start();
        }

        public void resetTimer() {
            realseconds = startSeconds;
            setText(formatTime(startSeconds));
        }

        public void setSelected(boolean choice) {
            if (choice) {
                getParent().repaint();
            }

            else {
                getParent().repaint();
            }
        }

        public void pause() {
            setSelected(false);
            pause = true;
        }

        public void unpause() {
            setSelected(true);
            pause = false;
        }

        public synchronized boolean getPause() {
            return pause;
        }

        public String formatTime(long seconds) {
            String time = null;

            if (seconds >= 60) {
                    minutes = seconds/60;
                    seconds = seconds - (minutes*60);
                    time = "" + minutes + ":" + String.format("%02d" ,seconds);
                }

                else if (seconds >= 3600) {
                    hours = minutes/60;
                    minutes = minutes - (hours*60);
                    seconds = seconds - (minutes*60);
                    time = "" + hours + String.format("%02d", minutes) + ":" + String.format("%02d" ,seconds);
                }

                else {
                    time = "" + String.format("%02d" ,seconds);
                }

            return time;
        }

        public void run() {
            while(true) {

                // if we're paused, go into a loop to suspend timer
                while (getPause()) {
                    try {
                        Thread.sleep(20);
                    } catch (Exception ex) {ex.printStackTrace();}
                }

                realseconds -= 1;
                seconds = realseconds;

                setText(formatTime(realseconds));
                getParent().getParent().repaint();

                try {
                    Thread.sleep(1000);
                } catch (Exception ex) {ex.printStackTrace();}
                }

        }
}
